package com.lbn.monitor.app.monitor.facility;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.free.framework.base.BaseFragmentPagerAdapter;
import com.free.framework.lib.utils.PrefsMgr;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbn.monitor.R;
import com.lbn.monitor.api.model.Pollutant;
import com.lbn.monitor.api.model.RealTimeValue;
import com.lbn.monitor.api.model.XiShu;
import com.lbn.monitor.app.history.adapter.PollutantAdapter;
import com.lbn.monitor.app.monitor.DataCallBack;
import com.lbn.monitor.base.AppBaseActivity;
import com.lbn.monitor.util.Constant;
import com.lbn.monitor.util.DataUtils;
import com.lbn.monitor.widget.Appbar;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import indi.free.widgets.custom_fragment.FragmentTabAdapter;
import indi.free.widgets.custom_recycler_view.BaseRecyclerAdapter;

public class FacilityDetailActivity extends AppBaseActivity<FacilityDetailPresenter> implements
        IFacilityDetailView, BaseRecyclerAdapter.OnItemClickListener ,BaseFragmentPagerAdapter.OnPageSelected{

    @BindView(R.id.pollutant_list)
    RecyclerView pollutantList;
    @BindView(R.id.drawer)
    DrawerLayout drawer;
    private List<DataCallBack> callBacks;
    private XiShu xishu;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.location)
    TextView location;
    private static final String TYPE = "TYPE";
    private static final String MN = "MN";
    private static final String DEVICE_Id = "DEVICE_Id";
    private static final String DATA_TYPE = "DATA_TYPE";
    private static final String LOCATION = "LOCATION";
    private int type; //1 原烟  2净烟
    private int dataType;
    private String mN;
    private String deviceId;
    private String locationStr;
    private List<Pollutant> pollutants;
    private Pollutant pollutant;
    private PollutantAdapter adapter;
    private List<Fragment> fragments;
    private RealTimeValue nowData,minData,hourData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facilit_detail);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra(TYPE, 0);
        mN = getIntent().getStringExtra(MN);
        deviceId = getIntent().getStringExtra(DEVICE_Id);
        dataType = getIntent().getIntExtra(DATA_TYPE, 0);
        locationStr = getIntent().getStringExtra(LOCATION);
        setAppbar(new Appbar(this)
                .openBack()
                .setTitle("实时数据")
//                .rightButton("污染物", new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (drawer.isDrawerOpen(GravityCompat.END)) {
//                            drawer.closeDrawer(GravityCompat.END);//关闭抽屉
//                        } else {
//                            drawer.openDrawer(GravityCompat.END);//打开抽屉
//                        }
//                    }
//                })
        );
        location.setText(locationStr);
        callBacks = Collections.synchronizedList(new ArrayList<DataCallBack>());
    }


    public static void startActivity(Context context,String location,int type, String mN, String deviceId, int dataType) {
        Intent intent = new Intent(context, FacilityDetailActivity.class);
        intent.putExtra(LOCATION,location);
        intent.putExtra(TYPE, type);
        intent.putExtra(MN, mN);
        intent.putExtra(DEVICE_Id, deviceId);
        intent.putExtra(DATA_TYPE, dataType);
        context.startActivity(intent);
    }

    @Override
    protected FacilityDetailPresenter createPresenter() {
        Log.e("FacilityDetailActivity", type + "createPresenter");
        return new FacilityDetailPresenter();
    }

    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();
        Log.e("FacilityDetailActivity", type + "onPresenterCreated");
        pollutants = DataUtils.getPollutants(this);
        initPollutantList();
        pollutant = pollutants.get(0);

        fragments = new ArrayList<>();

        switch (type) {
            case 1:
                fragments.add(RcNowFragment.newInstance());
                fragments.add(RcMinFragment.newInstance());
                fragments.add(RcHourFragment.newInstance());
                break;
            case 2:
                fragments.add(NcNowFragment.newInstance());
                fragments.add(NcMinFragment.newInstance());
                fragments.add(NcHourFragment.newInstance());
                break;
        }


//        switch (type) {
//            case 1:
//                fragments.add(RlcFragment.newInstance());//原烟折线图
//                fragments.add(RcFragment.newInstance());//原烟数据图
//                break;
//            case 2:
//                fragments.add(NlcFragment.newInstance());//净烟折线图
//                fragments.add(NcFragment.newInstance());//净烟数据图
//                break;
//        }
//        new FragmentTabAdapter(this, fragments, R.id.m_replace, radioGroup);
        viewPager.setOffscreenPageLimit(5);
        new BaseFragmentPagerAdapter(getSupportFragmentManager(),fragments,viewPager,radioGroup)
                .setOnPageSelected(this);
        viewPager.setCurrentItem(dataType);
        mPresenter.realTimeValue(mN, deviceId);
//        mPresenter.liuLiangXiShu(deviceId);
    }

    @Override
    public void onSucceed(List<RealTimeValue> data) {
        Log.e("okhttp", "RealTimeValue:" + data.size());

        for (RealTimeValue value : data) {
            Log.e("callBack",value.getDataCN());
            if (value.getDataCN().equals("2011")) {
                nowData = value;
                ((DataCallBack) fragments.get(0)).callBack(value);
            } else if (value.getDataCN().equals("2051")) {
                minData = value;
                ((DataCallBack) fragments.get(1)).callBack(value);
            } else if (value.getDataCN().equals("2061")) {
                hourData = value;
                ((DataCallBack) fragments.get(2)).callBack(value);
            }
        }
//        for (DataCallBack d : callBacks) {
//            d.callBack(data.get(0));
//        }
        hideLoadingView();
    }

    public Pollutant getPollutant() {
        return pollutant;
    }

    @Override
    public void onXiShuSucceed(XiShu xiShu) {
        this.xishu = xiShu;
        Log.e("okhttp", "xishu:" + new Gson().toJson(xiShu));
//        for (DataCallBack d : callBacks) {
//            d.callBack(xishu);
//        }
    }

    public void addCallBack(DataCallBack dataCallBack) {
        callBacks.add(dataCallBack);
    }

    public void getData() {
//        mPresenter.realTimeValue(mN);
    }

    public XiShu getXishu() {
        return xishu;
    }


    private void initPollutantList() {
        adapter = new PollutantAdapter(this, pollutants);
        pollutantList.setLayoutManager(new LinearLayoutManager(this));
        pollutantList.setHasFixedSize(true);
        pollutantList.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(View itemView, int pos) {
        pollutant = adapter.getItem(pos);
        for (DataCallBack d : callBacks) {
            d.clear();
        }
        if (drawer.isDrawerOpen(GravityCompat.END)) {
            drawer.closeDrawer(GravityCompat.END);//关闭抽屉
        }
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.END)) {
            drawer.closeDrawer(GravityCompat.END);//关闭抽屉
            return;
        }
        super.onBackPressed();
    }


    public RealTimeValue getNowData() {
        return nowData;
    }

    public RealTimeValue getMinData() {
        return minData;
    }

    public RealTimeValue getHourData() {
        return hourData;
    }

    @Override
    public void onPageSelected(int position) {
        String title = "";
        switch (position){
            case 0:
                title = "实时数据";
                break;
            case 1:
                title = "分钟数据";
                break;
            case 2:
                title = "小时数据";
                break;
        }
        ((Appbar)mFjrAppbar).setTitle(title);
    }
}
